package org.example.examfx02.controller;

import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import org.example.examfx02.entity.Course;
import org.example.examfx02.entity.Student;
import org.example.examfx02.HelloApplication;
import org.example.examfx02.tool.SystemData;
import org.example.examfx02.tool.WriteFile;

import java.util.List;

public class StuViewController {
    Student curUser = (Student)SystemData.getCurUser();
    SystemData sys = SystemData.getInstance();
    List<Course> list = sys.getCourseByStudentId(curUser.getId());
    @FXML
    Label welcomeText;
    @FXML
    ToggleButton saveBtn;
    @FXML
    Button chooseBtn;
    @FXML
    Button changeBtn;
    @FXML
    TableView table;
    @FXML
    TableColumn<Course,String> courseId;
    @FXML
    TableColumn<Course,String> courseName;
    @FXML
    TableColumn<Course,String> courseNum;
    @FXML
    TableColumn<Course,String> courseType;
    @FXML
    TableColumn<Course,Integer> courseCredit;
    @FXML
    TableColumn<Course,Integer> courseMaxNum;
    @FXML
    TableColumn<Course,String> courseTeacher;

    public void initTable(){
        courseId.setCellValueFactory(new PropertyValueFactory<>("id"));
        courseName.setCellValueFactory(new PropertyValueFactory<>("name"));
        courseNum.setCellValueFactory(new PropertyValueFactory<>("chooseNum"));
        courseTeacher.setCellFactory(column->new TeacherNameCell());
        courseType.setCellFactory(column->new TypeCell());
        courseCredit.setCellFactory(column -> new CreditCell());
        courseMaxNum.setCellFactory(column -> new MaxNumCell());
        // 为公共列设置居中显示
        courseId.setCellFactory(MainViewController.createCenteredCellFactory());
        courseName.setCellFactory(MainViewController.createCenteredCellFactory());
        courseNum.setCellFactory(MainViewController.createCenteredCellFactory());
        table.setItems(FXCollections.observableArrayList(list));
    }
    @FXML
    public void initialize(){
        welcomeText.setText("欢迎你,"+SystemData.getCurUser().getUsername()+"同学");
        initTable();
    }
    public void changePassword(){
        HelloApplication.addView("changePassword-view.fxml",400,290,"修改密码");
    }
    public void save(){
        WriteFile.writeStuFile(sys.getUserGroup().getStudents());
        WriteFile.writeSelectCourseFile(sys.getSc().getScList());
    }
    public void choose(){
        HelloApplication.addView("chooseCourse-view.fxml",700,430,"选课界面");
    }
}
